package com.mall.biz.weixin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.mall.vo.weixin.Template;
import com.mall.web.util.HttpUtil;
/**
 * 发送微信模板消息
 * @author lihaiyang
 * 
 */

@Service
public class WeiXinSendMessBizImpl {
	
	private static Logger log = LoggerFactory.getLogger(WeiXinSendMessBizImpl.class);
	
	
	
	
	public Template setTemplateMess(){
		Template tem=new Template();
		return tem;
	}
	
	/**
	 * 
	 * @param token//微信的access_token
	 * @param template //需要发送的模板消息
	 * @return
	 */
	public  boolean sendTemplateMsg(String token,Template template){  
        
        boolean flag=false;  
          
        String requestUrl="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";  
        requestUrl=requestUrl.replace("ACCESS_TOKEN", token);  
        net.sf.json.JSONObject jsonResult=HttpUtil.httpsRequest(requestUrl, "POST", template.toJSON());  
        if(jsonResult!=null){  
            int errorCode=jsonResult.getInt("errcode");  
            String errorMessage=jsonResult.getString("errmsg");  
            if(errorCode==0){  
                flag=true;  
            }else{  
            	log.error("模板消息发送失败:"+errorCode+","+errorMessage);
               // System.out.println("模板消息发送失败:"+errorCode+","+errorMessage);  
                flag=false;  
            }  
        }  
        return flag;  
	}  

}
